package com.tanhua.dubbo.api;

import cn.hutool.core.util.PageUtil;
import com.tanhua.model.db.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther : 白月尘
 * @date : 14:33  2021/10/25
 */
@DubboService
public class VideoApiImpl implements VideoApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    //上传视频
    @Override
    public String saveVideo(Video video) {
        Video save = mongoTemplate.save(video);
        return save.getId().toHexString();
    }

    //查找视频
    @Override
    public List<Video> findAll(Integer page, Integer pageSize) {
        Query query = new Query().with(Sort.by(Sort.Order.desc("created")))
                .skip((page - 1) * pageSize)
                .limit(pageSize);
        List<Video> list = mongoTemplate.find(query, Video.class);
        return list;
    }

    //根据ids查找视频
    @Override
    public List<Video> findByIds(String[] strings, Integer page, Integer pageSize) {
        List<Video> list = new ArrayList<>();
        if (page * pageSize < strings.length) {
            Criteria criteria = Criteria.where("id").in(strings);
            Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created")))
                    .skip((page - 1) * pageSize)
                    .limit(pageSize);
            list = mongoTemplate.find(query, Video.class);
        } else {
            int totalPage = PageUtil.totalPage(strings.length, pageSize);
            page = page - totalPage;
            list = findAll(page, pageSize);
        }

        return list;
    }

    //查询视频列表
    @Override
    public PageResult findVideoById(Integer page, Integer pageSize, long uid) {
        Criteria criteria = Criteria.where("userId").is(uid);
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, Video.class);
        query.with(Sort.by(Sort.Order.desc("created"))).skip((page - 1) * pageSize).limit(pageSize);
        List<Video> list = mongoTemplate.find(query, Video.class);
        return new PageResult(page, pageSize, count, list);
    }

    //评论列表
    @Override
    public List<Video> comment(Integer page, Integer pagesize, String id, String commentType) {
        // System.out.println(id);
        Criteria criteria = Criteria.where("id").is(new ObjectId(id)).and("commentType").is(commentType);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created"))).skip((page - 1) * pagesize).
                limit(pagesize);
        return mongoTemplate.find(query, Video.class);

    }
    /*//添加评论
    public Integer save(Video video) {
        //查询id设置到video对象
        ObjectId publishId = video.getId();
        Video video1 = mongoTemplate.findById(publishId, Video.class);
        video.setUserId(video1.getUserId());//设置动态发布人id
        //保存到mongodb中
        mongoTemplate.save(video);

        Criteria criteria = Criteria.where("id").is(publishId);
        Query query = Query.query(criteria);
        //修改动态表中的评论总数
        Update update = new Update();
        //update.set("commentCount",video1.getCommentCount()+1);
        if (video.getCommentType().equals("1")) { //点赞
            update.inc("likeCount", 1);
        } else if (video.getCommentType().equals("2")) {//评论
            update.inc("commentCount", 1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Video andModify = mongoTemplate.findAndModify(query, update, options, Video.class);
        if (video.getCommentType().equals("1")) { //点赞
            return andModify.getLikeCount();
        } else if (video.getCommentType().equals("2")) {//评论
            return andModify.getCommentCount();
        }
        return null;
    }*/

    @Override
    public Video findById(String id) {
        return mongoTemplate.findById(new ObjectId(id), Video.class);
    }
}
